
$(document).ready(function()
{
	var parkingId = $("#hidden_parking_id").val();
	
	if (parkingId != null && parkingId != "" && parkingId != "null")
	{
		getParking(parkingId);
	};
	
	$("#property_company").bind("change", function(){
		param = $(this).val();
		
		if (param != '-1')
		{
			loadCommunity(param,"");
		}
		else
		{
			$("#community").empty();
		}
		
		$("#building").empty();
		$("#unit").empty();
		$("#room").empty();
		$("#parking_type").empty();
		$("#plate_nbr").empty();
	});
	
	$("#community").bind("change", function(){
		param = $(this).val();
		
		if (param != '-1')
		{
			loadBuilding(param,"");
			loadParkingTypes(param,"");
		}
		else
		{
			$("#building").empty();
			$("#parking_type").empty();
		}
		
		$("#unit").empty();
		$("#room").empty();
		$("#plate_nbr").empty();
		
	});
	
	$("#building").bind("change", function(){
		param = $(this).val();
		
		if (param != '-1')
		{
			if (loadUnit(param,"") == '0')
			{
				$("#unit").empty();
				$("#unit").hide();
				$("#unitLabel").hide();
				loadRooms(param,"");
			}
		}
		else
		{
			$("#unit").empty();
		}
		
		$("#room").empty();
		$("#plate_nbr").empty();
	});
	
	$("#unit").bind("change", function(){
		param = $(this).val();
		
		if (param != '-1')
		{
			loadRooms(param,"");
		}
		else
		{
			$("#room").empty();
		}
		
		$("#plate_nbr").empty();
		
	});
	
	$("#room").bind("change", function(){
		param = $(this).val();
		if (param != '-1')
		{
			loadVehicle(param,"");
		}
		else
		{
			$("#plate_nbr").empty();
		}
		
	});	
			
});


function getParking(parking_id)
{
	$.post("parkingAction!getParking.action", {
		"inParam" : JSON.stringify({"parkingId":parking_id})
	}, 
	function(data) 
	{
		data = eval("(" + data + ")");
		
		if (data.found == "1") 
		{
			fillForm (data.data);
		} 
		else 
		{
			alert("未找到相关信息");
		}

	});
}

function fillForm (data)
{
	var ownerBuilding = "";
	$("#parking_nbr").val(data.parkingNbr);
	$("#parking_position").val(data.parkingPosition);
	
	loadCompany(data.companyId);
	loadCommunity(data.companyId, data.communityId);
	loadParkingTypes(data.communityId, data.parkingTypeId);
	
	if (data.vehicleId != null && data.vehicleId != "")
	{
		ownerBuilding = data.ownerBuilding;		
	}
	
	alert("ownerBuilding-->" + ownerBuilding);
	loadBuilding(data.communityId, ownerBuilding);
	
	if (data.vehicleId != null && data.vehicleId != "")
	{
		if (data.ownerBuilding == data.buildingId)
		{
			$("#unit").hide();
			$("#unitLabel").hide();
		}
		else
		{
			loadUnit(data.ownerBuilding, data.buildingId);
		}
		
		loadRooms(data.buildingId, data.roomId);
		loadVehicle(data.roomId, data.vehicleId);
	}
	
}

function loadCompany(selectedId)
{
	$.post("propertyCompanyAction!getPropertyInfo.action", {}
	, function(data)
	{
		data = eval("(" + data + ")");
		
		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite)
					{
						if (ite.id == selectedId)
						{
							option = option + '<option value="' + ite.id + '" selected="selected">' + ite.name + '</option>';
						}
						else
						{
							option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						}
						
					});
			
			
//			alert("option-->" + option);
			$("#property_company").empty().append(option);
			
		}
	});
}

function loadCommunity(companyId, selectedId)
{
	$.post("communityAction!getCommunityInfo.action", {inParma:JSON.stringify({"companyId":companyId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						if (ite.id == selectedId)
						{
							option = option + '<option value="' + ite.id + '" selected="selected">' + ite.name + '</option>';
						}
						else
						{
							option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						}
						
					});
			
			$("#community").empty().append(option);
			
		}
	});
}

function loadBuilding(communityId, selectedId)
{
	$.post("buildingAction!getBuildingInfo.action", {inParma:JSON.stringify({"communityId":communityId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据loadBuilding");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						if (ite.id == selectedId)
						{
							option = option + '<option value="' + ite.id + '" selected="selected">' + ite.name + '</option>';
						}
						else
						{
							option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						}
						
			});
			
			alert("option-->" + option);
			$("#building").empty().append(option);
			
		}
	});
}

function loadUnit(ownerBuildingId, selectedId)
{
	var rtnMsg = '0';
	$.post("buildingAction!getUnitInfo.action", {inParma:JSON.stringify({"ownerBuildingId":ownerBuildingId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
//			alert("未找到相关数据 loadUnit");
//			rtnMsg = '0';
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						if (ite.id == selectedId)
						{
							option = option + '<option value="' + ite.id + '" selected="selected">' + ite.name + '</option>';
						}
						else
						{
							option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						}
						
					});
			
			$("#unit").empty().append(option);
			$("#unit").show();
			$("#unitLabel").show();
			
			rtnMsg = '1';
			
		}
	});
	
	return rtnMsg;
}

function loadRooms(unitId, selectedId)
{
	$.post("roomAction!getRoomInfo.action", {inParma:JSON.stringify({"unitId":unitId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
//			alert("未找到相关数据loadRooms");
		}
		else
		{
			var option = '<option value="-1">请选择</option>';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						if (ite.id == selectedId)
						{
							option = option + '<option value="' + ite.id + '" selected="selected">' + ite.name + '</option>';
						}
						else
						{
							option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						}
						
					});
			
			$("#room").empty().append(option);
			
		}
	});
}

function loadParkingTypes(communityId, selectedId)
{
	$.post("parkingTypeAction!getParkingTypes.action", {inParma:JSON.stringify({"communityId":communityId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
		}
		else
		{
			var option = '';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						if (ite.id == selectedId)
						{
							option = option + '<option value="' + ite.id + '" selected="selected">' + ite.name + '</option>';
						}
						else
						{
							option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						}
						
					});
			
			$("#parking_type").empty().append(option);
			
		}
	});
}

function loadVehicle(roomId, selectedId)
{
	$.post("vehicleAction!getVehicleInfo.action", {inParma:JSON.stringify({"roomId":roomId})}
	, function(data)
	{
		data = eval("(" + data + ")");

		if (data.found == 0)
		{
			alert("未找到相关数据");
			$("#plate_nbr").empty();
		}
		else
		{
			var option = '';
			
			$(data.data).each
			(
					function(ix, ite) 
					{
						if (ite.id == selectedId)
						{
							option = option + '<option value="' + ite.id + '" selected="selected">' + ite.name + '</option>';
						}
						else
						{
							option = option + '<option value="' + ite.id + '">' + ite.name + '</option>';
						}
						
					});
			
			$("#plate_nbr").empty().append(option);
			
		}
	});
}

function updateParking()
{
	var parkingNbr = $("#parking_nbr").val();
	var parkingId = $("#hidden_parking_id").val();
	
	if (parkingNbr == null || parkingNbr == "null" || parkingNbr == "")
	{
		$.sucmodal("提示", "车位编码不能为空");
		
		return;
	}
	
	var communityId = $("#community").val();
	
	if (communityId == null || communityId == "null" || communityId == "")
	{
		$.sucmodal("提示", "小区不能为空");
		
		return;
	}

	var roomId = $("#room").val();
	var parkingTypeId = $("#parking_type option:selected").val();
	
	if (parkingTypeId == null || parkingTypeId == "null" || parkingTypeId == "")
	{
		$.sucmodal("提示", "车位类型不能为空");
		
		return;
	}
	
	var parkingPosition = $("#parking_position").val();
	var vehicleId = $("#plate_nbr").val();
	var plateNbr = $("#plate_nbr").find("option:selected").text();
	var jsarray = new Array();
	var parking = {};
	
	parking.parkingNbr = parkingNbr;
	parking.communityId = communityId;
	parking.parkingTypeId = parkingTypeId;
	parking.parkingPosition = parkingPosition;
	parking.parkingId = parkingId;	

	if (vehicleId != null && vehicleId != "" && vehicleId != "null")
	{
		if (roomId == null || roomId == "null" || roomId == "")
		{
			$.sucmodal("提示", "请选择车辆对应房间");
			
			return;
		}
		else
		{
			parking.vehicleId = vehicleId;
			parking.roomId = roomId;
			parking.plateNbr = plateNbr;
			
			jsarray.push(parking);
			
		}

	}
	else
	{
		if (confirm("未关联车牌号，继续提交吗？"))
		{
			jsarray.push(parking);
			
		}
		else
		{
			return;
		}
	}
	
	commit(jsarray);
	
}

function commit(inJsonArray)
{
	var inparam = {data:{}};
	inparam.data = inJsonArray;
	
	$.post("parkingAction!updateParking.action", 
			{inParam : JSON.stringify(inparam)}, 
			function(data) 
			{
				data = eval("(" + data + ")");
				
				if (data.ok == "1") 
				{
					$.sucmodal("提示", "操作成功");
				}
				else 
				{
					alert("保存失败");
				}
	});
}